
using System.Linq;
using UnityEngine;
using UnityEngine.Serialization;

namespace LS.GameKit
{
    [System.Serializable]
    public struct GameEnum
    {
        [SerializeField]
        private string m_EnumTypeName;
        
        [SerializeField]
        private string m_EnumName;

        [SerializeField]
        private bool m_LockType;
        
        public GameEnum(string enumTypeName, bool lockType)
        {
            m_EnumTypeName = enumTypeName;
            m_EnumName = string.Empty;
            this.m_LockType = lockType;
        }
        
        public GameEnum(string enumTypeName, string enumName, bool lockType)
        {
            m_EnumTypeName = enumTypeName;
            m_EnumName = enumName;
            this.m_LockType = lockType;
        }
        
        public GameEnum(string enumTypeName, int enumValue, bool lockType)
        {
            m_EnumTypeName = enumTypeName;
            m_EnumName = EnumMatrixConfig.InstanceObject?.GetName(enumTypeName, enumValue)?? string.Empty;
            this.m_LockType = lockType;
        }
        
        public GameEnum(string enumTypeName, string enumName)
        {
            m_EnumTypeName = enumTypeName;
            m_EnumName = enumName;
            this.m_LockType = false;
        }

        public GameEnum(string enumTypeName, int enumValue)
        {
            m_EnumTypeName = enumTypeName;
            m_EnumName = EnumMatrixConfig.InstanceObject?.GetName(enumTypeName, enumValue)?? string.Empty;
            this.m_LockType = false;
        }

        public bool TypeLocked()
        {
            return m_LockType;
        }
        
        public string EnumTypeName()
        {
            return m_EnumTypeName;
        }
        
        public string EnumName()
        {
            return m_EnumName;
        }

        public bool Available()
        {
            return EnumMatrixConfig.InstanceObject?.Contains(m_EnumTypeName, m_EnumName)?? false;
        }
        
        public int? EnumValue()
        {
            return EnumMatrixConfig.InstanceObject?.GetValue(m_EnumTypeName, m_EnumName);
        }
        
        public static explicit operator int(GameEnum gameEnum)
        {
            return gameEnum.EnumValue().GetValueOrDefault();
        }

        public static bool operator ==(GameEnum gameEnum1, GameEnum gameEnum2)
        {
            return gameEnum1.GetHashCode() == gameEnum2.GetHashCode();
        }
        
        public static bool operator !=(GameEnum gameEnum1, GameEnum gameEnum2)
        {
            return gameEnum1.GetHashCode() != gameEnum2.GetHashCode();
        }

        public override string ToString()
        {
            if(string.IsNullOrEmpty(m_EnumTypeName) && string.IsNullOrEmpty(m_EnumName))
                return string.Empty;
            else
                return $"{m_EnumTypeName}.{m_EnumName}";
        }

        public override int GetHashCode()
        {
            return ToString().GetHashCode();
        }
    }
}